package com.yc.supermarket.web.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import com.yc.supermarket.entity.Admin;
import com.yc.supermarket.service.IAdminBiz;
import com.yc.supermarket.service.impl.AdminBizImpl;
import com.yc.supermarket.util.StringUtil;
import com.yc.supermarket.vo.ResultVO;

@WebServlet("/admin")
public class AdminController extends BaseController {
    private IAdminBiz biz =new AdminBizImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          req.setCharacterEncoding("UTF-8");
          resp.setCharacterEncoding("UTF-8");
          String op=req.getParameter("op");
          if("add".equals(op)){
              doAdd(req,resp);
          }else if("findPage".equals(op)){
              doFindPage(req,resp);
          }else if("login".equals(op)){
              doLogin(req,resp);
          }else if("update".equals(op)){
              doUpadte(req,resp);
          }
          else {
              resp.getWriter().print("WeiGe: no such Methond ...");
          }
    }

    private void doLogin(HttpServletRequest req, HttpServletResponse resp) {
    }
    /**
     * 分页
     * */
    private void doFindPage(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        Admin admin = parseRequest(req,Admin.class);
        String page =req.getParameter("page");
        String size =req.getParameter("limit");
        System.out.println("fule:"+admin);
        System.out.println("hte:"+page+size);
        ResultVO vo =null;
        if(StringUtil.isNull(page,size)){
            vo=biz.find(admin);
            printToJson(resp,vo);
        }else {
            Map<String,Object> map = biz.findPage(admin,Integer.parseInt(page),Integer.parseInt(size));
            System.out.println("map:"+map);
            printToJson(resp,map);
        }

     }
    /**
     * 添加管理员
     * */
    private void doAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        Admin admin =parseRequest(req,Admin.class);
        System.out.println("why:"+admin);
        int vo =biz.add(admin);
        System.out.println("tjrjis s:"+vo);
        printToJson(resp,vo);
    }

    private void doUpadte(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        System.out.println("come the add");
        Admin admin =parseRequest(req,Admin.class);
        ResultVO vo =biz.updateState(admin);
        printToJson(resp,vo);
    }
}
